﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Assets.Scripts.System
{
    public class WeaponSystem : MonoSingleton<WeaponSystem>, IBaseSystem
    {
        public GameObject weaponTemplate;
        public List<WeaponItemInfo> weaponList;
        private Dictionary<string, WeaponItemInfo> weaponDict = new Dictionary<string, WeaponItemInfo>();

        private void Start()
        {
            foreach (var item in weaponList)
            {
                weaponDict.Add(item.weaponName, item);
            }
        }
        public Weapon GetWeaponTemplate(string weaponName)
        {
            Weapon weapon = null;
            if (weaponDict.ContainsKey(weaponName))
            {
                Sprite weaponSprite = weaponDict[weaponName].weaponGO.GetComponent<C_BasicAttribute>().C_Sprite;
                GameObject go = Instantiate(weaponTemplate);
                weapon = go.AddComponent<Weapon>();
                weapon.init(weaponName, weaponSprite);
            }
            return weapon;
        }
        public ComponentManager EquipWeapon(string weaponName, Vector2 handSlotPos, Transform tran = null)
        {
            ComponentManager manager = null;
            if (weaponDict.ContainsKey(weaponName))
            {
                manager = Instantiate(weaponDict[weaponName].weaponGO, tran).GetComponent<ComponentManager>();
                manager.gameObject.transform.localPosition = handSlotPos;
                manager?.Init();
                return manager;
            }
            return manager;
        }
        public void Open()
        {

        }
        public void Close()
        {

        }

        public void OnUpdate()
        {

        }
    }
    [Serializable]
    public class WeaponItemInfo
    {
        public string weaponName;
        public string weaponDesc;
        public GameObject weaponGO;
    }
}